﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeiXinPF.Common;

namespace WeiXinPF.Web.admin.article
{
    public partial class advertisement_list : Web.UI.ManagePage
    {
        protected int channel_id;
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int category_id;
        protected string channel_name = string.Empty;
        protected string property = string.Empty;
        protected string keywords = string.Empty;
        protected string prolistview = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.channel_id = MXRequest.GetQueryInt("channel_id");
            this.category_id = MXRequest.GetQueryInt("category_id");
            this.keywords = MXRequest.GetQueryString("keywords");
            this.property = MXRequest.GetQueryString("property");

            if (channel_id == 0)
            {
                JscriptMsg("频道参数不正确！", "back", "Error");
                return;
            }
            this.channel_name = new BLL.channel().GetChannelName(this.channel_id); //取得频道名称
            this.pageSize = GetPageSize(10); //每页数量
            this.prolistview = "Txt"; //显示方式
            if (!Page.IsPostBack)
            {
                //ChkAdminLevel("channel_" + this.channel_name + "_list", MXEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind(this.channel_id); //绑定类别
                RptBind(this.channel_id, this.category_id ,keywords);
            }
        }

        #region 绑定类别=================================
        private void TreeBind(int _channel_id)
        {
            Model.wx_userweixin weixin = GetWeiXinCode();

            BLL.advertisement bll = new BLL.advertisement();
            //DataTable dt = bll.GetWCodeList(weixin.id, 0, _channel_id);
            DataTable dt = bll.GetAllCateGory();

            this.ddlCategoryId.Items.Clear();
            this.ddlCategoryId.Items.Add(new ListItem("所有类别", ""));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                //int ClassLayer = int.Parse(dr["class_layer"].ToString());
                string Title = dr["title"].ToString().Trim();
                this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
                //if (ClassLayer == 1)
                //{
                //    this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
                //}
                //else
                //{
                //    Title = "├ " + Title;
                //    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                //    this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
                //}
            }
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(int _channel_id, int _category_id , string _keywords)
        {
            Model.wx_userweixin weixin = GetWeiXinCode();
            string where = "";
            this.page = MXRequest.GetQueryInt("page", 1);
            if (this.category_id > 0)
            {
                this.ddlCategoryId.SelectedValue = _category_id.ToString();
                where += " and categoryid = " +_category_id.ToString() + " ";
            }
            this.ddlProperty.SelectedValue = this.property;
            this.txtKeywords.Text = this.keywords;
            if (_keywords.Trim() != string.Empty)
            {
                where += " and title like '%" + _keywords + "%' ";
            }
            //图表或列表显示
            BLL.advertisement bll = new BLL.advertisement();
            DataSet artDs = bll.GetList(this.totalCount, " where wid=" + weixin.id.ToString() + " and isdelete=0 " + where,true);


            //BLL.article bll = new BLL.article();
            //DataSet artDs = bll.GetWCodeList(weixin.id, _channel_id, _category_id, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            //url链接地址处理
            if (artDs != null && artDs.Tables.Count > 0 && artDs.Tables[0].Rows.Count > 0)
            {
                DataRow dr;
                int count = artDs.Tables[0].Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    dr = artDs.Tables[0].Rows[i];
                    string linkUrl = MyCommFun.ObjToStr(dr["linkurl"]);

                    if (linkUrl.Length > 0)
                    {
                        //外链地址
                        dr["linkurl"] = "<span class=\"lianjie_wai\">[外]</span>" + " <a href=\"javascript:;\">" + linkUrl + "</a>";
                    }
                    else
                    {
                        dr["linkurl"] = "<span class=\"lianjie_ben\">[本]</span>" + " <a href=\"javascript:;\">暂无站内链接</a>";
                        //dr["link_url"] = "<span class=\"lianjie_ben\">[本]</span>" + " <a href=\"javascript:;\">" + MyCommFun.getWebSite() + "/detail.aspx?wid=" + MyCommFun.ObjToStr(dr["wid"]) + "&aid=" + dr["id"] + "</a>";
                    }
                }
                artDs.AcceptChanges();
            }

            switch (this.prolistview)
            {
                default:
                    this.rptList1.DataSource = artDs;
                    this.rptList1.DataBind();
                    Utils.WriteCookie("advertisement_list_view", "Txt", 14400);
                    break;

            }
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}&page={4}",
                _channel_id.ToString(), _category_id.ToString(), this.keywords, this.property, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

     
        #region 返回图文每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("advertisement_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //设置操作
        //protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        //{
        //    ChkAdminLevel("channel_" + this.channel_name + "_list", MXEnums.ActionEnum.Edit.ToString()); //检查权限
        //    int id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidId")).Value);
        //    BLL.article bll = new BLL.article();
        //    Model.article model = bll.GetModel(id);
        //    switch (e.CommandName)
        //    {
        //        case "lbtnIsMsg":
        //            if (model.is_msg == 1)
        //                bll.UpdateField(id, "is_msg=0");
        //            else
        //                bll.UpdateField(id, "is_msg=1");
        //            break;
        //        case "lbtnIsTop":
        //            if (model.is_top == 1)
        //                bll.UpdateField(id, "is_top=0");
        //            else
        //                bll.UpdateField(id, "is_top=1");
        //            break;
        //        case "lbtnIsRed":
        //            if (model.is_red == 1)
        //                bll.UpdateField(id, "is_red=0");
        //            else
        //                bll.UpdateField(id, "is_red=1");
        //            break;
        //        case "lbtnIsHot":
        //            if (model.is_hot == 1)
        //                bll.UpdateField(id, "is_hot=0");
        //            else
        //                bll.UpdateField(id, "is_hot=1");
        //            break;
        //        case "lbtnIsSlide":
        //            if (model.is_slide == 1)
        //                bll.UpdateField(id, "is_slide=0");
        //            else
        //                bll.UpdateField(id, "is_slide=1");
        //            break;
        //    }
        //    this.RptBind(this.channel_id, this.category_id, this.keywords);
        //}

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}",
                this.channel_id.ToString(), this.category_id.ToString(), txtKeywords.Text, this.property));
        }

        //筛选类别
        protected void ddlCategoryId_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}",
                this.channel_id.ToString(), ddlCategoryId.SelectedValue, this.keywords, this.property));
        }

        //筛选属性
        protected void ddlProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}",
               this.channel_id.ToString(), this.category_id.ToString(), this.keywords, ddlProperty.SelectedValue));
        }

        //设置文字列表显示
        //protected void lbtnViewTxt_Click(object sender, EventArgs e)
        //{
        //    Utils.WriteCookie("article_list_view", "Txt", 14400);
        //    Response.Redirect(Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}&page={4}",
        //        this.channel_id.ToString(), this.category_id.ToString(), this.keywords, this.property, this.page.ToString()));
        //}

        ////设置图文列表显示
        //protected void lbtnViewImg_Click(object sender, EventArgs e)
        //{
        //    Utils.WriteCookie("article_list_view", "Img", 14400);
        //    Response.Redirect(Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}&page={4}",
        //        this.channel_id.ToString(), this.category_id.ToString(), this.keywords, this.property, this.page.ToString()));
        //}

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("advertisement_page_size", _pagesize.ToString(), 43200);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}",
                this.channel_id.ToString(), this.category_id.ToString(), this.keywords, this.property));
        }

        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("channel_" + this.channel_name + "_list", MXEnums.ActionEnum.Edit.ToString()); //检查权限
            BLL.article bll = new BLL.article();
            Repeater rptList = new Repeater();
            switch (this.prolistview)
            {
                default:
                    rptList = this.rptList1;
                    break;
            }
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "保存" + this.channel_name + "频道内容排序"); //记录日志
            JscriptMsg("保存排序成功啦！", Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}",
                this.channel_id.ToString(), this.category_id.ToString(), this.keywords, this.property), "Success");
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("channel_" + this.channel_name + "_list", MXEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            BLL.advertisement bll = new BLL.advertisement();
            Repeater rptList = new Repeater();
            switch (this.prolistview)
            {
                default:
                    rptList = this.rptList1;
                    break;
            }
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.delete(id))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "删除【广告】" + this.channel_name + "频道内容成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("advertisement_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}",
                this.channel_id.ToString(), this.category_id.ToString(), this.keywords, this.property), "Success");
        }
    }
}